//
//  LXHomeManagementCell.m
//  EnterpriseEdition
//
//  Created by ypj on 2022/7/6.
//

#import "LXHomeManagementCell.h"
#import "QuanXianModel.h"
#import "HomeCategoryItemCell.h"
#import "LXHomeJingItemCell.h"
#define kItemHeight 70

#define kItemCount 4

@interface LXHomeManagementCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic) UICollectionView *collectionView;
/**展开收起*/
@property (nonatomic) UIButton *leftBtn,*rightBtn;
/**车辆/客户*/
@property (nonatomic) UIButton *car_customBtn;
@end


@implementation LXHomeManagementCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    
    if (self) {
        
        
        UILabel *tipslabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 13, 100, 20)];
        tipslabel.textColor = ColorWith333333;
        tipslabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
        self.tipslabel = tipslabel;
        [self.contentView addSubview:tipslabel];
        
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth - 135, self.tipslabel.Y - 3, 125, 24)];
        [self.contentView addSubview:view];
        
        CGFloat X = 0;
        
        
        
        for (int i = 0; i < 2; i++) {
            
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, 0, view.width/2, view.height)];
            [btn setTitle:@"" forState:UIControlStateNormal];
            [btn setTitleColor:ColorWithAAAAAA forState:UIControlStateNormal];
            [btn setTitleColor:ColorWith3C6CFC forState:UIControlStateSelected];
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            btn.tag = i;
            [btn addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            if (i == 0) {
                self.leftBtn = btn;
                btn.selected = YES;
                [btn setBackgroundColor:[ColorWith3C6CFC colorWithAlphaComponent:0.2]];
            }
            else {
                self.rightBtn = btn;
                [btn setBackgroundColor:UIColor.whiteColor];
            }
            [btn addCorner:2 AndLine:ColorWithF5F5F5];
            [view addSubview:btn];
            
            X = btn.X_width - 3;
        }
        
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        flowLayout.itemSize = CGSizeMake(kScreenWidth/kItemCount,kItemHeight);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.minimumLineSpacing= 0;
        flowLayout.minimumInteritemSpacing = 0;
        
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = UIColor.whiteColor;
        [self.contentView addSubview:self.collectionView];
        
        self.collectionView.scrollEnabled = NO;
        self.collectionView.frame = CGRectMake(0, tipslabel.Y_height + 10, kScreenWidth, 30);
        [self.collectionView registerClass:[LXHomeJingItemCell class] forCellWithReuseIdentifier:NSStringFromClass([LXHomeJingItemCell class])];
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCategoryItemCell  class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeCategoryItemCell class])];

        

    }
    return self;
}


- (void)setModel:(QuanXianModel *)model{
    _model = model;
    
    
    self.tipslabel.text = model.title;
    
    NSArray *ary = [model.selText componentsSeparatedByString:@","];
    [self.leftBtn setTitle:ary.firstObject forState:UIControlStateNormal];
    [self.rightBtn setTitle:ary.lastObject forState:UIControlStateNormal];
    // 左边按钮
    if (model.selWho ==1) {
        self.leftBtn.selected = YES;
        [self.leftBtn setBackgroundColor:[ColorWith3C6CFC colorWithAlphaComponent:0.2]];
        self.rightBtn.selected = NO;
        [self.rightBtn setBackgroundColor:UIColor.whiteColor];
    }
    else{
        self.rightBtn.selected = YES;
        [self.rightBtn setBackgroundColor:[ColorWith3C6CFC colorWithAlphaComponent:0.2]];
        self.leftBtn.selected = NO;
        [self.leftBtn setBackgroundColor:UIColor.whiteColor];
    }
    
    

    self.dataAry = model.children;
    
    CGFloat height = [LXHomeManagementCell getCellHeightWithDataAry:model.children];
    self.collectionView.height = height;
    [self.collectionView reloadData];
    
    
}

- (void)clickBtnAction:(UIButton *)sender{
    
    if(sender.isSelected)return;
    
    // 代办提醒
    if ([self.model.key isEqualToString:@"remind"]) {
        if (sender.tag == 0) {
            self.leftBtn.selected = YES;
            [self.leftBtn setBackgroundColor:[ColorWith3C6CFC colorWithAlphaComponent:0.2]];
            self.rightBtn.selected = NO;
            [self.rightBtn setBackgroundColor:UIColor.whiteColor];
            self.model.selWho = 1;
            self.model.height = 180;
        }
        else {
            self.rightBtn.selected = YES;
            [self.rightBtn setBackgroundColor:[ColorWith3C6CFC colorWithAlphaComponent:0.2]];
            self.leftBtn.selected = NO;
            [self.leftBtn setBackgroundColor:UIColor.whiteColor];
            self.model.selWho = 2;
            self.model.height = 102;
        }
        
    }
    // 经营状况
    else if([self.model.key isEqualToString:@"management"]){
        if (sender.tag == 0) {
            self.leftBtn.selected = YES;
            [self.leftBtn setBackgroundColor:[ColorWith3C6CFC colorWithAlphaComponent:0.2]];
            self.rightBtn.selected = NO;
            [self.rightBtn setBackgroundColor:UIColor.whiteColor];
            self.model.selWho = 1;
        }
        else {
            self.rightBtn.selected = YES;
            [self.rightBtn setBackgroundColor:[ColorWith3C6CFC colorWithAlphaComponent:0.2]];
            self.leftBtn.selected = NO;
            [self.leftBtn setBackgroundColor:UIColor.whiteColor];
            self.model.selWho = 2;
        }
        
        
    }
    
    if (self.ToChangeValue) {
        self.ToChangeValue(self.model);
    }
   
}


+(CGFloat)getCellHeightWithDataAry:(NSArray*)dataAry
{
    CGFloat height = dataAry.count/kItemCount *kItemHeight;
    if (dataAry.count%kItemCount !=0 ){
        height = (dataAry.count/kItemCount +1) *kItemHeight ;
    }
    return height + 40;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataAry.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    QuanXianSmallModel *item = self.dataAry[indexPath.row];
    LXHomeJingItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LXHomeJingItemCell class]) forIndexPath:indexPath];
    
    if ([item.menuId intValue] == 9999) {
        cell.topLabel.textColor = ColorWithFC9530;
        cell.bottomLabel.textColor = ColorWithFC9530;
    }
    else{
        cell.topLabel.textColor = ColorWith3C6CFC;
        cell.bottomLabel.textColor = ColorWithAAAAAA;
    }
    
    cell.topLabel.text = [NSString stringWithFormat:@"%@",item.remindNum];
    cell.bottomLabel.text = [NSString stringWithFormat:@"%@",item.name];
    return cell;

   
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    QuanXianSmallModel *item = self.dataAry[indexPath.row];
    if (self.ToSendValue) {
        self.ToSendValue(item);
    }
    
}



@end
